package cn.imvc.app.models;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.AttrClass;
import cn.imvc.app.entities.Brand;
import cn.imvc.app.entities.BrandGoodsClass;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsAttrValue;
import cn.imvc.app.entities.GoodsClass;
import cn.imvc.app.entities.GoodsClassAttr;
import cn.imvc.app.entities.Storage;
import cn.imvc.app.repositories.GoodsAttrValueRepository;
import cn.imvc.app.repositories.GoodsClassAttrRepository;
import cn.imvc.app.repositories.GoodsRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年8月4日下午3:00:20
 *
 */
@Service
public class GoodsClassAttrModelImpl implements GoodsClassAttrModel{
 
	@Autowired
	GoodsClassAttrRepository goodsClassAttrRepository;
	@Autowired
	RefRelationModel refRelationModel;
	@Autowired
	GoodsRepository goodsRepository;
	@Autowired
	GoodsAttrValueRepository goodsAttrValueRepository;

	public Page<GoodsClassAttr> find(ViewRequest viewRequest) {
		
		return goodsClassAttrRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public GoodsClassAttr get(int id) {
		
		return goodsClassAttrRepository.getOne(id);
	}
	
	public int save(int goodsClassId, GoodsClassAttr goodsClassAttr) {
		GoodsClass goodsClass = goodsClassAttr.getGoodsClass();
		String attrName = goodsClassAttr.getAttrName();
		List<GoodsClassAttr> gn = goodsClassAttrRepository.getByGoodsClassAndAttrName(goodsClass,attrName);
		if(gn.size()>0){
			return -1;
		}
		goodsClassAttrRepository.saveAndFlush(goodsClassAttr);
		return 1;
		
	}

	public int update(int id,int goodsClassId,GoodsClassAttr goodsClassAttr) {
		GoodsClassAttr loadGoodsClassAttr = goodsClassAttrRepository.getOne(id);
		GoodsClass goodsClass = goodsClassAttr.getGoodsClass();
		String attrName = goodsClassAttr.getAttrName();
		List<GoodsClassAttr> gn = goodsClassAttrRepository.getByGoodsClassAndAttrName(goodsClass,attrName);
		if(gn.size()>0&&!attrName.equals(loadGoodsClassAttr.getAttrName())){
			return -1;
		}
		BeanUtils.copyProperties(goodsClassAttr, loadGoodsClassAttr, new String[]{"id"});
		goodsClassAttrRepository.saveAndFlush(loadGoodsClassAttr);
		return 1;
		
	}

	public String delete(int id) {
		String result = refRelationModel.deleteDateCheck("Goods_Class_Attr", "id", id);
		if(result.equals("")){
			goodsClassAttrRepository.delete(id);
			return "";
		}else{
			return result;
		}
		
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			goodsClassAttrRepository.delete(id);
		}
		
	}

	/**
	 * 根据商品分类的选择，自动更新分类属性关系关联表。
	 * ！该功能的使用应放在商品更新保存前。
	 *  @param oper：1增改，0删除
	 */
	public void updateRelationByGoods(Goods goods, int oper) {
		if(goods.getId() != 0){
			Goods goodsTmp = goodsRepository.findOne(goods.getId());
			if(goods.getGoodsClass().getId() != goodsTmp.getGoodsClass().getId()){
				List<GoodsAttrValue> goodsAttrValues = goodsAttrValueRepository.findByGoodsId(goods.getId());
				Iterator<GoodsAttrValue> iterator = goodsAttrValues.iterator();
				while (iterator.hasNext()) {
					GoodsAttrValue goodsAttrValue = (GoodsAttrValue) iterator.next();
					if(oper == 1){
						List<GoodsClassAttr> goodsClassAttrs = goodsClassAttrRepository.findByGoodsClassAndAttr(goods.getGoodsClass(), goodsAttrValue.getAttrClass());
						if(goodsClassAttrs.isEmpty()){
							GoodsClassAttr goodsClassAttr = new GoodsClassAttr();
							goodsClassAttr.setAttr(goodsAttrValue.getAttrClass());
							goodsClassAttr.setAttrName(goodsAttrValue.getAttrClass().getName());
							goodsClassAttr.setGoodsClass(goods.getGoodsClass());
							goodsClassAttr.setClassName(goods.getGoodsClass().getName());
							goodsClassAttrRepository.saveAndFlush(goodsClassAttr);
						}
					}
					if(goodsAttrValueRepository.findByGoodsClass(goodsTmp.getGoodsClass().getId(), goodsAttrValue.getAttrClass()).size() == 1){
						goodsClassAttrRepository.deleteByGoodsClassAndAttr(goodsTmp.getGoodsClass(),goodsAttrValue.getAttrClass());
					}
				}
			}
		}
//		Goods goods = goodsRepository.findOne(goodsAttrValue.getId());
//		if(oper == 1){
//			List<GoodsClassAttr> goodsClassAttrs = goodsClassAttrRepository.findByGoodsClassAndAttr(goods.getGoodsClass(), goodsAttrValue.getAttrClass());
//			if(goodsAttrValue.getId() != 0){
//				GoodsAttrValue goodsAttrValueTmp = goodsAttrValueRepository.findOne(goodsAttrValue.getId());
//				//如果更新的品牌类别关联在商品中不存在了，则将品牌类别关联表中的关联数据给删了。
//				if(goodsAttrValueTmp.getBrand().getId() != goods.getBrand().getId() || goodsTmp.getGoodsClass().getId() != goods.getGoodsClass().getId()){
//					if(goodsRepository.findByGoodsClassAndBrand(goodsTmp.getGoodsClass(), goodsTmp.getBrand()).size() == 1){
//						brandGoodsClassRepository.deleteByGoodsClassAndBrand(goodsTmp.getGoodsClass(), goodsTmp.getBrand());
//					}
//				}
//			}
//			//如果是新添加的品牌类别关联，则向品牌类别关联表添加一条关联数据。
//			if(brandGoodsClasses.isEmpty()){
//				BrandGoodsClass brandGoodsClass = new BrandGoodsClass();
//				Brand brand = brandModel.get(goods.getBrand().getId());
//				GoodsClass goodsClass = goodsClassModel.get(goods.getGoodsClass().getId());
//				brandGoodsClass.setBrand(brand);
//				brandGoodsClass.setBrandName(brand.getName());
//				brandGoodsClass.setGoodsClass(goodsClass);
//				brandGoodsClass.setGoodsClassName(goodsClass.getName());
//				brandGoodsClassRepository.saveAndFlush(brandGoodsClass);
//			}
//		}
	}

	@Override
	public void updateRelationByAttrClass(GoodsAttrValue goodsAttrValue, int oper) {
		Goods goods = goodsRepository.findOne(goodsAttrValue.getGoodsId());
		if(goodsAttrValue.getId() != 0){
			GoodsAttrValue goodsAttrValueTmp = goodsAttrValueRepository.findOne(goodsAttrValue.getId());
			if(goodsAttrValue.getAttrClass().getId() != goodsAttrValueTmp.getAttrClass().getId() || oper == 0){
				if(goodsAttrValueRepository.findByGoodsClass(goods.getGoodsClass().getId(), goodsAttrValue.getAttrClass()).size() == 1){
					goodsClassAttrRepository.deleteByGoodsClassAndAttr(goods.getGoodsClass(),goodsAttrValue.getAttrClass());
				}
			}
		}
		if(oper == 1){
			List<GoodsClassAttr> goodsClassAttrs = goodsClassAttrRepository.findByGoodsClassAndAttr(goods.getGoodsClass(), goodsAttrValue.getAttrClass());
			if(goodsClassAttrs.isEmpty()){
				GoodsClassAttr goodsClassAttr = new GoodsClassAttr();
				goodsClassAttr.setAttr(goodsAttrValue.getAttrClass());
				goodsClassAttr.setAttrName(goodsAttrValue.getAttrClass().getName());
				goodsClassAttr.setGoodsClass(goods.getGoodsClass());
				goodsClassAttr.setClassName(goods.getGoodsClass().getName());
				goodsClassAttrRepository.saveAndFlush(goodsClassAttr);
			}
		}
	}
	
}
